package com.weetop.aklyscan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bigkoo.pickerview.TimePickerView;
import com.blankj.utilcode.util.StringUtils;
import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.weetop.aklyscan.R;
import com.weetop.aklyscan.base.BaseActivity;
import com.weetop.aklyscan.base.mvp.BaseModel;
import com.weetop.aklyscan.bean.RecordListBean;
import com.weetop.aklyscan.bean.THRecordBean;
import com.weetop.aklyscan.presenter.RecordPresent;
import com.weetop.aklyscan.utils.BaseUtils;
import com.weetop.aklyscan.utils.MyTimePicker1;
import com.weetop.aklyscan.view.RecordView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class THRecordActivity extends BaseActivity<RecordPresent> implements RecordView {
    @BindView(R.id.startTime)
    TextView startTime;
    @BindView(R.id.stopTime)
    TextView stopTime;
    @BindView(R.id.num)
    TextView num;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private CommonRecyclerAdapter<THRecordBean.DataBean> adapter;
    private List<THRecordBean.DataBean> list = new ArrayList<>();
    private String order_sn;

    @Override
    protected RecordPresent createPresenter() {
        return new RecordPresent(this, this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_th_record);
        ButterKnife.bind(this);
        order_sn = getIntent().getStringExtra("order_sn");
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new CommonRecyclerAdapter<THRecordBean.DataBean>(mActivity, R.layout.item_th_record, list) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, THRecordBean.DataBean item, int position) {
                helper.setText(R.id.tv_id, position + 1 + "");
                helper.setText(R.id.time, item.getRk_time());
                helper.setText(R.id.number, item.getCount_num());
                helper.setText(R.id.code, item.getOrder_sn());
                helper.setText(R.id.user, item.getApply_user_name());
                helper.setOnClickListener(R.id.ll, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(THRecordActivity.this, THRecordListActivity.class)
                                .putExtra("order_sn",order_sn)
                                .putExtra("time",item.getRk_time()));
                    }
                });
            }
        };
        recyclerView.setAdapter(adapter);
        startTime.setText(getTime());
        mPresenter.Into77();
    }

    String endTime;
    @Override
    public void getInto77Success(BaseModel<String> o) {
        endTime = o.getData();
    }


    @Override
    public void getpub03Success(BaseModel<THRecordBean> o) {
        list = o.getData().getList_data();
        adapter.clear();
        adapter.addAll(list);
        num.setText(list.size() + "");
    }

    @OnClick({R.id.iv_back, R.id.startTime, R.id.stopTime, R.id.chaxun})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.startTime:
                MyTimePicker1 myTimePicker1 = new MyTimePicker1();
                Calendar endCalendar;
                if(StringUtils.isEmpty(endTime)){
                    endCalendar=  BaseUtils.dateToCalendar();
                }else{
                    endCalendar= BaseUtils.StringTocalendar(endTime);
                }
                TimePickerView pickerView1 = myTimePicker1.selectTime(THRecordActivity.this,
                        BaseUtils.StringTocalendar("2019-1-1"), endCalendar);
                myTimePicker1.setOnSelectListener(new MyTimePicker1.OnSelectListener() {
                    @Override
                    public void onSelect(Date date, View v) {
                        startTime.setText(BaseUtils.dateToStrLong(date, "yyyy-MM-dd"));
                    }
                });
                pickerView1.show();
                break;
            case R.id.stopTime:
                MyTimePicker1 myTimePicker2 = new MyTimePicker1();
                Calendar endCalendar1;
                if(StringUtils.isEmpty(endTime)){
                    endCalendar1=  BaseUtils.dateToCalendar();
                }else{
                    endCalendar1= BaseUtils.StringTocalendar(endTime);
                }
                TimePickerView pickerView2 = myTimePicker2.selectTime(THRecordActivity.this,
                        BaseUtils.StringTocalendar(startTime.getText().toString().trim()), endCalendar1);
                myTimePicker2.setOnSelectListener(new MyTimePicker1.OnSelectListener() {
                    @Override
                    public void onSelect(Date date, View v) {
                        stopTime.setText(BaseUtils.dateToStrLong(date, "yyyy-MM-dd"));
                    }
                });
                pickerView2.show();
                break;
            case R.id.chaxun:
                if (TextUtils.isEmpty(stopTime.getText().toString().trim())) {
                    mPresenter.pub03("1", order_sn, startTime.getText().toString().trim() + " 00:00:00", getTime() + " 23:59:59");
                } else {
                    mPresenter.pub03("1", order_sn, startTime.getText().toString().trim() + " 00:00:00", stopTime.getText().toString().trim() + " 23:59:59");
                }
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

    public String getTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        return formatter.format(date);
    }

}
